const request = require("./request.js")
const MS = require('./ms')
const {
    isString,
    isNumber,
    isObject,
    isUndefined,
    isBoolean
} = require('./dataType')

const goodsCategory = class goodsCategory {

    loadGoodsCategorys() {
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.SHOP.LOAD_GOODS_CATEGORY.url,
                MS._config.parameters.SHOP.LOAD_GOODS_CATEGORY.method, {}, {}).then(res => {
                resolve(res.data)
            }).catch(err => {
                reject(err);
            })
        })
    }

    getGoodsSubCategory(subCategoryId) {
        let params = {
            subCategoryId: subCategoryId
        }
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.SHOP.GET_GOODS_SUB_CATEGORY.url,
                MS._config.parameters.SHOP.GET_GOODS_SUB_CATEGORY.method, params, {}).then(res => {
                resolve(res.data)
            }).catch(err => {
                reject(err);
            })
        })
    }

    query(options) {
        let params = {
            pageNum: 1,
            pageSize: 10
        }
        if (!isUndefined(options.name) && isString(options.name)) {
            params.name = options.name;
        }
        if (!isUndefined(options.pageNum) && isNumber(options.pageNum)) {
            params.pageNum = options.pageNum;
        }
        if (!isUndefined(options.pageSize) && isNumber(options.pageSize)) {
            params.pageSize = options.pageSize;
        }
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.GOODS_CATEGORY.FIND_GOODS_CATEGORY.url,
                MS._config.parameters.GOODS_CATEGORY.FIND_GOODS_CATEGORY.method, params).then(res => {
                if (res.success) {
                    resolve(res.data)
                } else {
                    reject(res.message)
                }
            }).catch(err => {
                reject(err)
            })
        })
    }

    getGoodsCategory(id) {
        let params = {
            goodsCategoryId: id
        }
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.GOODS_CATEGORY.GET_GOODS_CATEGORY.url,
                MS._config.parameters.GOODS_CATEGORY.GET_GOODS_CATEGORY.method, params).then(res => {
                if (res.success) {
                    resolve(res.data)
                } else {
                    reject(res.message)
                }
            }).catch(err => {
                reject(err)
            })
        })
    }

    findGoodsSubCategory(goodsCategoryId) {
        let params = {
            goodsCategoryId: goodsCategoryId
        }
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.GOODS_CATEGORY.FIND_GOODS_SUB_CATEGORY.url,
                MS._config.parameters.GOODS_CATEGORY.FIND_GOODS_SUB_CATEGORY.method, params).then(res => {
                if (res.success) {
                    resolve(res.data)
                } else {
                    reject(res.message)
                }
            }).catch(err => {
                reject(err)
            })
        })
    }

    saveGoodsCategory(data) {
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.GOODS_CATEGORY.SAVE_GOODS_CATEGORY.url,
                MS._config.parameters.GOODS_CATEGORY.SAVE_GOODS_CATEGORY.method, {}, data).then(res => {
                if (res.success) {
                    resolve(res.data)
                } else {
                    reject(res.message)
                }
            }).catch(err => {
                reject(err)
            })
        })
    }

    saveGoodsSubCategory(data) {
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.GOODS_CATEGORY.SAVE_GOODS_SUB_CATEGORY.url,
                MS._config.parameters.GOODS_CATEGORY.SAVE_GOODS_SUB_CATEGORY.method, {}, data).then(res => {
                if (res.success) {
                    resolve(res.data)
                } else {
                    reject(res.message)
                }
            }).catch(err => {
                reject(err)
            })
        })
    }

    removeGoodsSubCategory(goodsSubCategoryId) {
        let params = {
            goodsSubCategoryId: goodsSubCategoryId
        }
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.GOODS_CATEGORY.REMOVE_GOODS_SUB_CATEGORY.url,
                MS._config.parameters.GOODS_CATEGORY.REMOVE_GOODS_SUB_CATEGORY.method, params).then(res => {
                if (res.success) {
                    resolve(res.success)
                } else {
                    reject(res.message)
                }
            }).catch(err => {
                reject(err)
            })
        })
    }
}

module.exports = goodsCategory
